const { DataTypes } = require('sequelize');
const sequelize = require('../config/db');

const Order = sequelize.define('Order', {
    id: {
        type: DataTypes.BIGINT,
        primaryKey: true,
        autoIncrement: true,
        comment: '订单 ID'
    },
    order_no: {
        type: DataTypes.STRING(32),
        allowNull: false,
        unique: true,
        comment: '订单编号(业务唯一)'
    },
    user_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '用户 ID'
    },
    total_amount: {
        type: DataTypes.DECIMAL(12, 2),
        allowNull: false,
        comment: '订单总金额'
    },
    deposit_amount: {
        type: DataTypes.DECIMAL(12, 2),
        allowNull: false,
        comment: '总押金'
    },
    payment_method_type: {
        type: DataTypes.TINYINT,
        defaultValue: 0,
        comment: '支付方式类型:1-支付宝 2-微信 3-银行卡'
    },
    order_status: {
        type: DataTypes.TINYINT,
        allowNull: false,
        comment: '订单状态:0-待支付 1-已支付 2-租赁中 3-已完成 4-已取消'
    },
    rent_start_date: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '租赁开始日期'
    },
    rent_end_date: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '租赁结束日期'
    },
    actual_rent_days: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '实际租赁天数'
    },
    delivery_address: {
        type: DataTypes.TEXT,
        comment: '配送地址(JSON 存储详细信息)'
    },
    product_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '商品 ID'
    },
    quantity: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 1,
        comment: '租赁数量'
    },
    item_deposit: {
        type: DataTypes.DECIMAL(10, 2),
        allowNull: false,
        comment: '单项押金'
    },
    user_notes: {
        type: DataTypes.TEXT,
        comment: '用户备注'
    },
    cancel_reason: {
        type: DataTypes.STRING(255),
        defaultValue: '',
        comment: '取消原因'
    }
}, {
    tableName: 'order',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at'
});

module.exports = Order;
